package org.jdsm.gae.komarichyn.blog;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.ActionProxy;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import com.opensymphony.xwork2.util.LocalizedTextUtil;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.gae.dispatcher.multipart.GaeMultiPartRequestWrapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * User: mikom
 * Date: 11/14/12
 * Time: 3:05 PM
 */
public class FileManagerInterceptor extends AbstractInterceptor {
    private static final Logger log = LoggerFactory.getLogger(FileManagerInterceptor.class);

    @Override
    public String intercept(ActionInvocation invocation) throws Exception {
        ActionContext ac = invocation.getInvocationContext();
        HttpServletRequest request = (HttpServletRequest) ac.get(ServletActionContext.HTTP_REQUEST);

        //application/x-www-form-urlencoded

        if (!"application/x-www-form-urlencoded".equalsIgnoreCase(request.getContentType())) {
            if (log.isDebugEnabled()) {
                ActionProxy proxy = invocation.getProxy();
                log.debug(getTextMessage("struts.messages.bypass.request", new Object[]{proxy.getNamespace(), proxy.getActionName()}, ac.getLocale()));
            }

            return invocation.invoke();
        }


        // bind allowed Files
        Mode mode = parseRequest(request);
        if(mode != null){
            Map<String, Object> params = ac.getParameters();
            params.put("mode", mode);
        }

        // invoke action
        String result = invocation.invoke();

        // cleanup

        return result;

    }

    private Mode parseRequest(HttpServletRequest request) {
        Mode result = null;
        String modeParam = request.getParameter("mode");
        if ("cfg".equals(modeParam)) {
            Mode mode = new Mode();
            mode.setLang(request.getParameter("lang"));
            mode.setType(request.getParameter("type"));
            result = mode;
        } else if ("getDirs".equals(modeParam)) {
            DirMode mode = new DirMode();
            mode.setDir(request.getParameter("dir"));
            mode.setLang(request.getParameter("lang"));
            mode.setType(request.getParameter("type"));
            result = mode;
        } else if ("getFiles".equals(modeParam)) {
            FileMode mode = new FileMode();
            mode.setDir(request.getParameter("dir"));
            mode.setLang(request.getParameter("lang"));
            mode.setType(request.getParameter("type"));
            mode.setSort(request.getParameter("sort"));
            result = mode;
        }
        return result;
    }


    protected boolean useActionMessageBundle;
    private static final String DEFAULT_MESSAGE = "no.message.found";


    private String getTextMessage(String messageKey, Object[] args, Locale locale) {
        return getTextMessage(null, messageKey, args, locale);
    }

    private String getTextMessage(Object action, String messageKey, Object[] args, Locale locale) {
        if (args == null || args.length == 0) {
            if (action != null && useActionMessageBundle) {
                return LocalizedTextUtil.findText(action.getClass(), messageKey, locale);
            }
            return LocalizedTextUtil.findText(this.getClass(), messageKey, locale);
        } else {
            if (action != null && useActionMessageBundle) {
                return LocalizedTextUtil.findText(action.getClass(), messageKey, locale, DEFAULT_MESSAGE, args);
            }
            return LocalizedTextUtil.findText(this.getClass(), messageKey, locale, DEFAULT_MESSAGE, args);
        }
    }
}
